import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { App } from 'vue'
import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper'
import { setRequestRouter } from '@/request/request'

const NotFound = () => import('@/views/NotFound/notFound.vue')
const Home = () => import('@/views/Home/home.vue')

const Authority = () => import('@/views/Example/Authority/index.vue')
const FoldPanel = () => import('@/views/Example/FoldPanel/index.vue')
const HtmlShower = () => import('@/views/Example/HtmlShower/index.vue')
const InputItem = () => import('@/views/Example/InputItem/index.vue')
const JsShower = () => import('@/views/Example/JsShower/index.vue')

const LocalNoRightRoutes: RouteRecordRaw[] = [
  { path: '/', redirect: '/home' },
  { path: '/home', name: 'home', component: Home },
  { path: '/example/authority', name: 'authority', component: Authority },
  { path: '/example/foldPanel', name: 'foldPanel', component: FoldPanel },
  { path: '/example/htmlShower', name: 'htmlShower', component: HtmlShower },
  { path: '/example/inputItem', name: 'inputItem', component: InputItem },
  { path: '/example/jsShower', name: 'jsShower', component: JsShower },
  { path: '/:catchAll(.*)*', name: '404', component: NotFound },
]

//
//
// 默认路由页面
let routes: RouteRecordRaw[] = LocalNoRightRoutes

const router = createRouter({
  // 判断是否在qiankun环境里，xxx与主应用中activeRule一致
  history: createWebHistory(
    qiankunWindow.__POWERED_BY_QIANKUN__ ? '/subApplication/vueComponent' : '/vueComponent'
  ),
  routes,
})

// 封装路由初始化方法
export const initRouter = (app: App<Element>) => {
  app.use(router)
}
// 传递路由给axios实例使用
setRequestRouter(router)
